<?php
class Receive extends MY_Controller {
				public  $current_controller;
        function __construct() {
            parent::__construct();
                    $this->load->model('master/metermaster_model');
                    $this->load->model('electric_meter/submeter_model');
                    $this->load->model('master/corporate_model');
                    $this->load->model('grid_model');
										$this->current_controller = $this->router->class;
                    $this->load->helper('common');

        }
        function index($offset = 0)
        {
				   $data['title']= $this->my_controller->name;
					$this->template->write('title',$data['title']);
	        $this->template->write_view('content', "repair/{$this->current_controller}_list",$data);
	        $this->template->render();
        }
		
 		function add()
 		{
  
			//$this->submeter_model->delete($fccode,$date,$mtcode);	
			//print_r($this->input->post('rmu'));
			$date=$this->input->post('date');
			$fccode=$this->input->post('fccode');
			$bdcode=$this->input->post('bdcode');
			$entry_date=convert2save($this->input->post('entry_date'));	
			$mmcode=$this->input->post('mmcode');
			$arr=explode( '-' , $date ); //
			 
			$year=$this->input->post('year');
			$month=$this->input->post('month');
	 		$unit=$this->input->post('unit');
				
			$last=$this->input->post('sub_last');
			 
			$meter=$this->metermaster_model->get_by_id($mmcode);

            if  ($meter->ISDECIMAL=="Y")
                  if ($unit<$last) $meter_diff=($meter->MAX_UNIT-$last)+$unit+0.1;
                    else  $meter_diff=$unit-$last;
                  else
                     if ($unit<$last) $meter_diff=($meter->MAX_UNIT-$last)+$unit+1;
                     else  $meter_diff=$unit-$last;
			$meter=$this->metermaster_model->get_by_id($mmcode,$arr[0],$arr[1]);
			$items = array('mmcode' =>$meter->MMCODE,
                                               'mrcode' => $meter->MRCODE,
					       'entry_year' =>$arr[1],
					       'entry_month' => $arr[0],
                                               'entry_week' =>$arr[2],
					       'entry_date' => $entry_date,
                                               'meter_unit' =>(float)$unit,
                                               'meter_diff'=>(float)$meter_diff,
                                               'fix_rate' => $meter->FIX_RATE,
                                               'user_modify' => $this->session->userdata('username'),
					       'date_modify' => date('Y-m-d H:i:s')
			);
			$this->submeter_model->save($items);         
            $this->view($fccode,$bdcode,$mmcode,$year,$month,'');
 		} 

 		function delete()
 		{
			$fccode=$this->input->post('fccode');	
			$bdcode=$this->input->post('bdcode');
			$year=$this->input->post('year');
			$month=$this->input->post('month');	
			$mmcode=$this->input->post('mmcode');		
			$date=$this->input->post('date');
			$arr=explode( '-' , $date ); //	
			$this->submeter_model->delete($mmcode,$arr[1],$arr[0],$arr[2]);
            $this->view($fccode,$bdcode,$mmcode,$year,$month,'');
 		}
        function approve()
 		{
			$fccode=$this->input->post('fccode');
			$bdcode=$this->input->post('bdcode');
			$year=$this->input->post('year');
			$month=$this->input->post('month');
			$mmcode=$this->input->post('mmcode');
			$date=$this->input->post('date');
			$arr=explode( '-' , $date ); //
			$this->submeter_model->approve($mmcode,$arr[0],$arr[1],$arr[2],$this->session->userdata('username'));
            $this->view($fccode,$bdcode,$mmcode,$year,$month,'');
 		}
        function update_remark()
 		{
			$fccode=$this->input->post('fccode');
			$bdcode=$this->input->post('bdcode');
			$year=$this->input->post('year');
			$month=$this->input->post('month');
			$date=$this->input->post('date');
			$mmcode=$this->input->post('mmcode');
			$remark=$this->input->post('remark');
			$arr=explode( '-' , $date ); //
			$this->submeter_model->update_remark($mmcode,$arr[1],$arr[0],$arr[2],$remark);		 
			$this->view($fccode,$bdcode,$mmcode,$year,$month,'');
 		}
  		function view($fccode,$bdcode,$mmcode,$year,$month,$date)
 		{
	        $data['month'] =echo_month("month",$month);
	        $data['year'] =echo_year("year",$year);
	        $data['date']=$date;
	        $data['mmcode']=$mmcode;
	        $building=$this->submeter_model->get_building($fccode);
	        $data['fccode'] =echo_dropdown_list("fccode",$this->corporate_model->get_corp_user($this->session->userdata('username')),"FCCODE","FCNAME",$fccode,false,"style='width:370px' onchange='change_corporate()'");
	        $data['bdcode']=$bdcode;
	        $data['building']=echo_tab_list("bdcode",$building,"BDCODE","BDNAME",$bdcode);
	        $data['title']= $this->my_controller->name;
	        $data['action'] = site_url($this->my_controller->controller)."#".$mmcode;
	        $data['table']=$this->submeter_model->generate_meter($fccode,$bdcode,$mmcode,$year,$month,$date,$this->my_controller->can_approve);	                        
	        $this->template->write('title',$data['title']);
	        $this->template->write_view('content', 'electric_meter/submeter_list',$data);
	        $this->template->render();
 		}
     
}
?>